<link rel="stylesheet" href="../static/css/style.css">
<link rel="stylesheet" href="../static/css/styleoneadd.css">
<link rel="stylesheet" href="../static/plugins/layui/css/layui.css">
<link rel="stylesheet" href="../static/plugins/bootstrap-3.3.7/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../static/plugins/bootstrap-3.3.7/css/bootstrap.min.css">
<style>
    .layui-form-checked[lay-skin=primary] i{
        border-color: #34C0E3 !important;
        background-color: #34C0E3 !important;
        color: #fff;
    }
    .layui-form-checked, .layui-form-checked:hover{
        border-color: #34C0E3 !important;
    }
</style>
    <!-- 内容主体区域 -->
<div class="layui-row layui-pad15" ng-app="get_uniterm" ng-controller="add_select" id="add_select">
    <form class="layui-form" action="" lay-filter="test_form">
            <div class="layui-col-md12 layui-col-lh">
                <div class="layui-row layui-col-space10">
                     <div class="layui-col-xs3">
                         <input type="text" name="title" ng-model="itermName" placeholder="单项名称" class="form-control">
                     </div>
                    <div class="layui-col-xs4">
                        <div class="layui-row">
                            <div class="layui-col-xs12 layui-order-flexs">
                                <input type="text" class="form-control layui-gd-widths" placeholder="时间" id="test1">
                                &nbsp;&nbsp;至&nbsp;&nbsp;
                                <input type="text" class="form-control layui-gd-widths" placeholder="时间" id="test2">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs1 layui-text-right">
                        <button type="button" class="layui-btn  layui-t-h" ng-click="screen()">筛选</button>
                    </div>
                </div>
            </div>
            <div class="layui-col-md12 layui-col-lh">
                <div class="layui-row">
                    <div class="layui-col-xs12">
                        <table class="layui-data-zs" id="test3" border="1" style="    border: 1px solid #e5e5e5;">
                            <thead>
                                <tr class="layui-t-h layui-hg" style=" background-color: #34C0E3;color: #ffffff;">
                                    <th>&nbsp;&nbsp;</th>
                                    <th>单项名称</th>
                                    <th>单项价格</th>
                                    <th>添加时间</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr class="layui-hg" ng-repeat="question in localItermList">
                                    <td><input type="checkbox" lay-skin="primary" name="unitermId" select="wtname" lay-ignore style="display: inline-block;" value="{{question.id}}"/></td>
                                    <td><span ng-bind="question.name || '--'"></span></td>
                                    <td><span ng-bind="(question.price / 100 + '.' + question.price % 100) || '--'"></span></td>
                                    <td><span ng-bind="(question.createdTime|date:'yyyy-MM-dd') || '--'"></span></td>
                                </tr>
                            </tbody>
                        </table>
                        <div ng-show="!localItermList || localItermList.length == 0" style="margin: 20px 0; text-align: center;">无符合条件数据。</div>
                    </div>
                </div>
            </div>
            <!--分页-->
            <div class="layui-col-md12 layui-text-center" ng-hide="!localItermList || localItermList.length == 0">
                <div id="demo7"></div>
            </div>
            <!--分页end-->

            <div class="layui-col-md12  layui-text-center layui-col-lh">
                <a href="#" class="layui-q-c layui-zdy-bj" ng-click="getIds()">确定</a>
            </div>
    </form>


</div>
<script src="../static/js/jquery.min.js"></script>
<script src="../static/plugins/layui/layui.js"></script>
<script src="../static/plugins/bootstrap-3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../static/js/angular.js"></script>
<script src="../static/js/app/consts/consts.js"></script>
<script>
    $(function () {
        // 表格点击选中
        $('#test3').on('click','tbody tr',function(){

            var check = $(this).find('input[select=wtname]').prop('checked');

            if(check == true){
                $(this).find('input[select=wtname]').prop('checked',false);
            }else{
                $(this).find('input[select=wtname]').prop('checked',true);
            }

        })
    })
    var pageConut = 0;
    var pageSize = 10;
    var pageNum = 1;
    var currentPageNumber = 1;

    var getAllLocalUnitermURL = baseUrl + "/exam/getAllLocalUniterm";
    var getPartlLocalUnitermURL = baseUrl + "/exam/getPartlLocalUniterm";

    var app = angular.module('get_uniterm', ['ng']);
    app.controller('add_select', function ($scope, $http) {

        var parent_num = 0;

        $scope.getIds = function () {
            var ids = document.getElementsByName("unitermId");
            var check_val = [];
            var uids = "";
            for(var k in ids){
                if(ids[k].checked) {
                    check_val.push(ids[k].value);
                    uids += ids[k].value + ","
                }
            }
            console.log(parent_num);
            var id = "localUniterm" + parent_num;
            var str = uids.substr(0, uids.length - 1);
//            console.log(uids.substr(0, uids.length - 1));
            window.parent.document.getElementById(id).value = str;
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        }

        $(function () {
            var url = location.search;
            console.log(url);
            var num = "";
            if(url.indexOf("?") != -1) {
                var str = url.substr(1);
                num = str.split("=")[1];
            }
            console.log(num);
            parent_num = num;
        });

        $http({
            method: 'GET',
            params:{
                token:"11111",
                pageNumber: pageNum,
                pageSize: pageSize,
                sort: "",
                desc: ""
            },
            url: getAllLocalUnitermURL
        }).then(function successCallback(result){
            if(result.data) {
                var resultInfo = result.data.data;
                updateUnitermPagination(resultInfo);
                if(resultInfo) {
                    $scope.localItermList = resultInfo.data;
                }
                if(result.data.msg) {
                    layer.msg(result.data.msg)
                }
            }
        },function errorCallback(result) {
            console.log("error");
        });

        //筛选按钮
        $scope.screen = function (goPageNumber) {
            if(!goPageNumber) {
                pageNum = 1;
            } else {
                pageNum = goPageNumber;
            }
            var data = {
                token:"22222",
                localUnitermName:$scope.itermName?($scope.itermName):"",
                startTime:$('#test1').val()?($('#test1').val()):"",
                endTime:$('#test2').val()?($('#test2').val()):"",
                code:"",
                pageNumber: pageNum,
                pageSize: pageSize,
                sort: "",
                desc: ""
            };
            console.log(data);
            $http({
                method:"POST",
                url:getPartlLocalUnitermURL,
                data:data,
                headers:{'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
                transformRequest: function(obj) {
                    var str = [];
                    for(var p in obj){
                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                    }
                    return str.join("&");
                }
            }).then(
                function successCallback(result) {
                    if(result.data) {
                        var resultInfo = result.data.data;
                        updateUnitermPagination(resultInfo);
                        if(resultInfo) {
                            $scope.localItermList = resultInfo.data;
                            console.log($scope.localItermList);
                        }
//                        if(result.data.msg) {
//                            layer.msg(result.data.msg)
//                        }
                    }
                },
                function errorCalback(result) {
                    console.log("error");
                }
            );
        }



//        layui.use(['form'],function () {
//           var form = layui.form;
//           form.render(null, 'test_form');
//        });
    });

    //更新分页数据
    function updateUnitermPagination(resultInfo) {
        pageConut = resultInfo.total || 0;
        pageSize = resultInfo.pageSize || 10;
        pageNum = resultInfo.pageNumber || 1;
        refreshPagination();
    }

    // 分页刷新
    function refreshPagination() {
        layui.use(['laypage', 'layer'], function() {
            var laypage = layui.laypage;
            //分页
            laypage.render({
                elem: 'demo7'
                ,count: pageConut
                ,curr: pageNum
                ,limit: pageSize
                ,theme: '#34C0E3'
                ,layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
                ,jump: function(obj, first) {
                    if (!first) {
                        pageNum = obj.curr;
                        pageSize = obj.limit;
                        var scope = getAngularScope();
                        scope.screen(pageNum);
                    }
//                    console.log(pageSize + ":" + pageNum)
                }
            });
        });
    }

    // 获取angular scope
    function getAngularScope() {
        return angular.element(unitermManage).scope();
    }

    //Demo
    layui.use(['form','laypage','laydate','table','layer'], function(){
        var form = layui.form;
        var laydate = layui.laydate;
        var laypage = layui.laypage

        $(function () {
            form.render(null, 'test_form');
        });

        //监听提交
        form.on('submit(formDemo)', function(data){
            layer.msg(JSON.stringify(data.field));
            return false;
        });

        // 日期
        laydate.render({
            elem: '#test1' //指定元素
            ,theme: '#34C0E3'
        });
        laydate.render({
            elem: '#test2' //指定元素
            ,theme: '#34C0E3'
        });
        //分页
//        laypage.render({
//            elem: 'demo7'
//            ,count: 100
//            ,layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
//            ,jump: function(obj){
//                console.log(obj)
//            }
//        });

    });
</script>


